using System;
using Boo.Lang.Compiler.Ast;
using Rhino.DSL;

namespace Mite.DSL
{
	public class MiteMethodSubstitutionBaseClassCompilerStep : MethodSubstitutionBaseClassCompilerStep
	{
		public MiteMethodSubstitutionBaseClassCompilerStep(Type baseClassType, params string[] namespaces)
			: base(baseClassType, namespaces)
		{
		}

		public MiteMethodSubstitutionBaseClassCompilerStep(Type baseClassType, DepthFirstTransformer[] visitors,
		                                                   params string[] namespaces)
			: base(baseClassType, visitors, namespaces)
		{
		}

		protected override void ExtendBaseClass(Module module, ClassDefinition definition)
		{
			base.ExtendBaseClass(module, definition);

			bool containsVersion =
				definition.Members.ToArray().Contains(
					member => member.NodeType == NodeType.Method && member.Name.Equals("Version"));
			if (!containsVersion)
			{
				var versionReturn = new ReturnStatement(definition.LexicalInfo);
				versionReturn.Expression = new IntegerLiteralExpression(definition.LexicalInfo, definition.Name.GetVersion());

				var versionGetter = new Method(definition.LexicalInfo);
				versionGetter.Name = "getter_Version";
				versionGetter.Body = new Block(definition.LexicalInfo);
				versionGetter.Body.Add(versionReturn);

				var version = new Property(definition.LexicalInfo);
				version.Name = "Version";
				version.Modifiers = TypeMemberModifiers.Public | TypeMemberModifiers.Override;
				version.Getter = versionGetter;

				definition.Members.Add(version);
			}
		}
	}
}